﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PowerManager.Framework.DataPresentation.ExchangeableTarget;
using Lextm.SharpSnmpLib.Messaging;
using System.Net;
using Lextm.SharpSnmpLib;

namespace PowerManager.Framework.DataTransfer
{
    internal static class SnmpDataHelper
    {
        public static String GetSingle(IExchangeableTarget target, IPAddress ipAddress) {            
            var oid = new ObjectIdentifier(target.ObjectIdentifier);
            IList<Variable> variables = new List<Variable> { new Variable(oid) };
            IList<Variable> result = new List<Variable>();
            
            var retry = PMSettings.Retry;
            var timeout = PMSettings.Timeout;
            var version = PMSettings.Version;
            var community = new OctetString(PMSettings.Community);
            var port = PMSettings.Port;
            var iep = new IPEndPoint(ipAddress, port);

            for (int i = 0; i < retry; i++) {
                try {
                    result = Messenger.Get(version, iep, community, variables, timeout);
                } catch {
                    result = null;
                }
                if (result != null) {
                    break;
                }
            }

            return result.Count < 1 ? null : result.First().Data.ToString();
        }

        public static IList<String[]> GetTable(IExchangeableTarget target, IPAddress ipAddress) {
            var oid = new ObjectIdentifier(target.ObjectIdentifier);
            var variables = new List<Variable>();
            var retry = PMSettings.Retry;
            var timeout = PMSettings.Timeout;
            var version = PMSettings.Version;
            var community = new OctetString(PMSettings.Community);
            var port = PMSettings.Port;
            var iep = new IPEndPoint(ipAddress, port);

            for (Int32 i = 0; i < retry; i++) {
                try {
                    Messenger.Walk(version, iep, community, oid, variables, timeout, WalkMode.WithinSubtree);
                } catch {
                    variables = null;
                }
                if (variables != null) {
                    break;
                }
            }

            return variables.Count < 1 ? null : TableParse(variables, ((TableExchangeableTarget)target).Column);
        }

        private static IList<String[]> TableParse(List<Variable> variables, Int32 column) {
            if (variables == null) {
                return null;
            }
            int rowCount = variables.Count / column;
            var result = new List<String[]>();
            for (int i = 0; i < rowCount; i++) {
                var variable = new String[column];
                for (int j = 0; j < column; j++) {
                    variable[j] = variables[j * rowCount + i].Data.ToString();
                }
                result.Add(variable);
            }
            return result;
        }
    }
}
